﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Entities;

namespace InsuranceVehicle
{
    public partial class RegisterCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                addListItem();
            }
        }

        private void addListItem()
        {
            for (int i = 1; i <= 12; i++)
            {
                cbbMonth.Items.Add(i.ToString());
            }

            for (int i = 1; i <= 31; i++)
            {
                cbbDay.Items.Add(i.ToString());
            }

            int curYear = DateTime.Now.Year;
            for (int i = curYear - 80; i <= curYear - 18; i++)
            {
                cbbYear.Items.Add(i.ToString());
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Page.Validate();
            CustomerDataAccess db = new CustomerDataAccess();
            CustomerInfo c = new CustomerInfo();
            lblError.Visible = false;
            if (IsValid)
            {
                c.Firstname = txtFirstname.Text;
                c.Lastname = txtLastname.Text;
                c.PersonalIdentity = txtPersonalIdentity.Text;
                c.Account = txtAccount.Text;
                c.Password = txtPasword.Text;
                c.Job = txtJob.Text;
                c.Dob = cbbMonth.SelectedItem.ToString() + "/" +
                    cbbDay.SelectedItem.ToString() + "/" + cbbYear.SelectedItem.ToString();
                c.Address = txtAddress.Text;
                c.Phone = txtPhone.Text;
                c.Email = txtEmail.Text;
                
                int errCode = db.CustomerRegister(c);
                
                string message = "";
                switch (errCode)
                {
                    case 1:
                        Response.Write("<script>alert('Customer register successful')</script>");
                        break;
                    case 2:
                        message = "Connection error with database ";
                        break;
                    case 3:
                        message = "Account is existed";
                        txtAccount.Focus();
                        break;
                    case 4:
                        message = "Personal identity number is really used";
                        txtPersonalIdentity.Focus();
                        break;
                    case 5:
                        message = "Email is really used";
                        txtEmail.Focus();
                        break;
                    default:
                        message = "";
                        break;
                }
                lblError.Visible = true;
                lblError.Text = message;
            }
           
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtFirstname.Text = "";
            txtLastname.Text = "";
            txtPersonalIdentity.Text = "";
            txtAccount.Text = "";
            txtPasword.Text = "";
            txtConPass.Text = "";
            txtJob.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
        }
    }
}